package com.itheima.sky.controller.backend;


import com.itheima.sky.dto.DishDto;
import com.itheima.sky.pojo.Dish;
import com.itheima.sky.vo.DishFlavorVO;
import com.itheima.sky.entity.Result;
import com.itheima.sky.service.DishFlavorService;
import com.itheima.sky.service.DishService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description DishController
 * @Author p
 * @Date 2024-06-28
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishControllerBack {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result page(int page, int pageSize, String name){
        return  dishService.page(page,pageSize,name);
    }

    /**
     * 作用：根据id查找菜品和菜品的口味
     * @param id  菜品的id
     * @return
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Long id) {
        DishDto dishDto = dishService.findById(id);
        return Result.success(dishDto);
    }

    /**
     * 作用：修改菜品
     * @param dishDto 页面传递过来的参数包含菜品与口味
     * @return
     */
    @PutMapping
    public Result update(@RequestBody  DishDto dishDto,HttpSession session) {
        //1. 获取当前登陆者,设置当前的修改人
        Long empId = (Long) session.getAttribute("empId");
        log.info(dishDto.toString());
        dishDto.setUpdateUser(empId);
        dishService.updateWithFlavor(dishDto);
        return Result.success("修改菜品成功");
    }


    /**
     * 作用：保存菜品并且携带口味信息
     * @param dishDto  用于接收菜品与口味信息
     * @return
     */
    @PostMapping
    public Result save(@RequestBody  DishDto dishDto, HttpSession session){
        //1. 获取当前登陆者,补全创建者与更新者信息
        Long empId = (Long) session.getAttribute("empId");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        //2. 保存菜品与口味
        dishService.saveWithFlavor(dishDto);
        return Result.success("保存菜品成功");
    }


    //o
    /**
     * 根据菜品类别的id查找的菜品
     * @param categoryId
     * @return
     */
    //以下内容转为在front controller实现
//    @GetMapping("/list")
//    public Result list(Long categoryId, String name){
//
//
//        List<Dish> dishList = dishService.findByCategoryId(categoryId, name);
//
//        return Result.success(dishList);
//
//    }

}
